home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4664 / 4664.xpi / chrome / twitterbar.jar / content / overlay.js < prev    next >
Text File  |  2010-02-08  |  21KB  |  646 lines

  1. var TWITTERBAR = {
  2.     debug : false,
  3.     
  4.     lastTweet : null,
  5.     covertMode : false,
  6.     
  7.     version : null,
  8.     
  9.     lastUrl : null,
  10.     
  11.     load : function () {
  12.         this.version = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager).getItemForID("{1a0c9ebe-ddf9-4b76-b8a3-675c77874d37}").version;
  13.         
  14.         TWITTERBAR.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.twitter.");    
  15.         TWITTERBAR.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
  16.         TWITTERBAR.prefs.addObserver("", this, false);
  17.         
  18.         TWITTERBAR.debug = TWITTERBAR.prefs.getBoolPref("debug");
  19.         
  20.         var showFirstRun = false;
  21.         var oldVersion = TWITTERBAR.prefs.getCharPref("version");
  22.         var newVersion = this.version;
  23.         
  24.         if (oldVersion != newVersion) {
  25.             TWITTERBAR.prefs.setCharPref("version", newVersion);
  26.         }
  27.         
  28.         if (!oldVersion) {
  29.             showFirstRun = true;
  30.         }
  31.         else {
  32.             var oldParts = oldVersion.split(".");
  33.             var newParts = newVersion.split(".");
  34.         
  35.             if (newParts[0] != oldParts[0] || newParts[1] != oldParts[1]) {
  36.                 showFirstRun = true;
  37.             }
  38.         }
  39.         
  40.         if (showFirstRun) {
  41.             var browser = getBrowser();
  42.             
  43.             setTimeout(function (browser) {
  44.                 browser.selectedTab = browser.addTab("http://www.chrisfinke.com/firstrun/twitterbar.php?v=" + newVersion);
  45.             }, 3000, browser);
  46.         }
  47.         
  48.         var engineLabel = TWITTERBAR_COMMON.strings.getString("twitter.search.name");
  49.         
  50.         if (!TWITTERBAR.prefs.getBoolPref("search_request")) {
  51.             TWITTERBAR.prefs.setBoolPref("search_request", true);
  52.             
  53.             setTimeout(
  54.                 function installSearch() {
  55.                     var searchService = Components.classes["@mozilla.org/browser/search-service;1"];
  56.                     
  57.                     if (searchService) {
  58.                         searchService = searchService.getService(Components.interfaces.nsIBrowserSearchService);
  59.                         
  60.                         var oneRiotSearch = searchService.getEngineByName(engineLabel);
  61.                         
  62.                         if (oneRiotSearch == null) {
  63.                             window.openDialog("chrome://twitterbar/content/OneRiotSearchDialog-twitterbar-ff.xul", "search", "chrome,dialog,centerscreen,titlebar,alwaysraised,modal");
  64.                         }
  65.                     }
  66.                 }, 5000);
  67.         }
  68.         else if (!TWITTERBAR.prefs.getBoolPref("search_fixed")) {
  69.             TWITTERBAR.prefs.setBoolPref("search_fixed", true);
  70.             
  71.             try {
  72.                 // Check if an old search version is installed.
  73.                 var searchService = Components.classes["@mozilla.org/browser/search-service;1"];
  74.  
  75.                 if (searchService) {
  76.                     searchService = searchService.getService(Components.interfaces.nsIBrowserSearchService);
  77.  
  78.                     var oneRiotSearch = searchService.getEngineByName(engineLabel);
  79.                 
  80.                     if (oneRiotSearch) {
  81.                         var testSubmission = oneRiotSearch.getSubmission("test", null);
  82.                         var searchUrl = testSubmission.uri.spec;
  83.  
  84.                         // The old one uses a versioned parameter, which is confusing because it doesn't update as the user updates their browser.
  85.                         if (searchUrl.indexOf("twitterbar-ff/") != -1) {
  86.                             var isDefault = false;
  87.                         
  88.                             if (searchService.currentEngine == oneRiotSearch) {
  89.                                 // Set this engine back as the default.
  90.                                 isDefault = true;
  91.                             }
  92.                         
  93.                             searchService.removeEngine(oneRiotSearch);
  94.                             searchService.addEngineWithDetails(engineLabel, "http://www.oneriot.com/images/favicon.ico", null, document.getElementById("twitterbar-strings").getString("twitter.search.description"), "get", "http://www.oneriot.com/search?q={searchTerms}&format=html&ssrc=browserBox&spid=86f2f5da-3b24-4a87-bbb3-1ad47525359d&p=twitterbar-ff");
  95.                         
  96.                             if (isDefault) {
  97.                                 var newOneRiotSearch = searchService.getEngineByName(engineLabel);
  98.                                 searchService.currentEngine = newOneRiotSearch;
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.             } catch (e) {
  104.             }
  105.         }
  106.     
  107.         document.getElementById("urlbar").addEventListener("keyup", function (event) { TWITTERBAR.postKey(event); }, false);
  108.         document.getElementById("urlbar").addEventListener("focus", function () { TWITTERBAR.focus(); }, false);
  109.         
  110.         this.buttonCheck();
  111.         
  112.         var appcontent = document.getElementById("content");
  113.         
  114.         if (appcontent) {
  115.             appcontent.addEventListener("DOMContentLoaded", TWITTERBAR.DOMContentLoaded, true);
  116.         }
  117.         
  118.         // Get new trends every 2 hours.
  119.         TWITTERBAR.trendTimer = setInterval(function () { TWITTERBAR_COMMON.getTrends(); }, 1000 * 60 * 60 * 2);
  120.         
  121.         setTimeout(function() { TWITTERBAR_COMMON.getTrends(); }, 1000 * 10);
  122.     },
  123.     
  124.     unload : function () {
  125.         TWITTERBAR.prefs.removeObserver("", this);
  126.         
  127.         var appcontent = document.getElementById("content");
  128.         
  129.         if (appcontent) {
  130.             appcontent.removeEventListener("DOMContentLoaded", TWITTERBAR.DOMContentLoaded, true);
  131.         }
  132.         
  133.         clearInterval(TWITTERBAR.trendTimer);
  134.     },
  135.     
  136.     observe : function(subject, topic, data) {
  137.         if (topic != "nsPref:changed") {
  138.             return;
  139.         }
  140.         
  141.         switch(data) {
  142.             case "button":
  143.                 // Iterate over all the windows and show/hide the button based on pref-hide-button
  144.                 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  145.                                    .getService(Components.interfaces.nsIWindowMediator);
  146.                 var enumerator = wm.getEnumerator(null);
  147.  
  148.                 var buttonMode = TWITTERBAR.prefs.getBoolPref("button").toString();
  149.  
  150.                 while(enumerator.hasMoreElements()) {
  151.                     var win = enumerator.getNext();
  152.  
  153.                     try {
  154.                         win.document.getElementById("twitterBox").setAttribute("hidden", buttonMode);
  155.                     } catch (e) { }
  156.                 }
  157.             break;
  158.             case "oneriotButton":
  159.                 // Iterate over all the windows and show/hide the button based on pref-hide-button
  160.                 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  161.                                    .getService(Components.interfaces.nsIWindowMediator);
  162.                 var enumerator = wm.getEnumerator(null);
  163.  
  164.                 var buttonMode = TWITTERBAR.prefs.getBoolPref("oneriotButton").toString();
  165.  
  166.                 while(enumerator.hasMoreElements()) {
  167.                     var win = enumerator.getNext();
  168.  
  169.                     try {
  170.                         win.document.getElementById("twitter-oneriot-box").setAttribute("hidden", buttonMode);
  171.                     } catch (e) { }
  172.                 }
  173.             break;
  174.             case "debug":
  175.                 TWITTERBAR.debug = TWITTERBAR.prefs.getBoolPref("debug");
  176.             break;
  177.         }
  178.     },
  179.     
  180.     DOMContentLoaded : function (event) {
  181.         if (event.originalTarget instanceof HTMLDocument) {
  182.             var page = event.originalTarget;
  183.             
  184.             if (page.location.href.match(/chrisfinke.com\/oauth\/twitterbar/i)) {
  185.                 var urlArgs = page.location.href.split("?")[1].split("&");
  186.                 
  187.                 var token = "";
  188.                 
  189.                 for (var i = 0; i < urlArgs.length; i++) {
  190.                     var argParts = urlArgs[i].split("=");
  191.                     
  192.                     if (argParts[0] == "oauth_token"){
  193.                         token = argParts[1];
  194.                     }
  195.                 }
  196.                 
  197.                 var accessor = {
  198.                     consumerSecret : TWITTERBAR_COMMON.oauth.consumer_secret,
  199.                     tokenSecret : TWITTERBAR_COMMON.oauth.request_token.oauth_token_secret
  200.                 };
  201.  
  202.                 var message = {
  203.                     action : TWITTERBAR_COMMON.oauth.serviceProvider.accessTokenURL,
  204.                     method : "GET",
  205.                     parameters : [
  206.                         ["oauth_consumer_key",TWITTERBAR_COMMON.oauth.consumer_key],
  207.                         ["oauth_token", token],
  208.                         ["oauth_signature_method",TWITTERBAR_COMMON.oauth.serviceProvider.signatureMethod],
  209.                         ["oauth_version","1.0"]
  210.                     ]
  211.                 };
  212.                 
  213.                 var OAuth = TWITTERBAR_OAUTH();
  214.                 
  215.                 OAuth.setTimestampAndNonce(message);
  216.                 OAuth.SignatureMethod.sign(message, accessor);
  217.                 
  218.                 var oAuthArgs = OAuth.getParameterMap(message.parameters);
  219.                 var authHeader = OAuth.getAuthorizationHeader("http://twitter.com/", oAuthArgs);
  220.                 
  221.                 var req = new XMLHttpRequest();
  222.                 req.mozBackgroundRequest = true;
  223.                 req.open(message.method, message.action, true);
  224.                 req.setRequestHeader("Authorization", authHeader);
  225.                 
  226.                 req.onreadystatechange = function () {
  227.                     if (req.readyState == 4) {
  228.                         if (TWITTERBAR.debug) {
  229.                             TWITTERBAR.log("Auth (DOM) ("+req.status+"): " + req.responseText);
  230.                         }
  231.                         
  232.                         if (req.status == 200) {
  233.                             try {
  234.                                 var parts = req.responseText.split("&");
  235.                                 
  236.                                 TWITTERBAR.prefs.setCharPref("access_token.oauth_token", parts[0].split("=")[1]);
  237.                                 TWITTERBAR.prefs.setCharPref("access_token.oauth_token_secret", parts[1].split("=")[1]);
  238.                                 
  239.                                 TWITTERBAR.prefs.setCharPref("oauth_timestamp", (new Date().getTime()));
  240.                                 
  241.                                 if (TWITTERBAR.lastTweet) {
  242.                                     TWITTERBAR.covertMode = true;
  243.                                     TWITTERBAR.postRequest(TWITTERBAR.lastTweet);
  244.                                 }
  245.                             } catch (e) {
  246.                                 TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getFormattedString("twitterbar.otherError", [ e, req.responseText ]));
  247.                             }
  248.                         }
  249.                         else if (req.status >= 500) {
  250.                             TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getString("twitterbar.failWhale"));
  251.                         }
  252.                         else {
  253.                             TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getFormattedString("twitterbar.otherError", [ req.status, req.responseText ]));
  254.                         }
  255.                     }
  256.                 };
  257.                 
  258.                 req.send(null);
  259.             }
  260.             else if (TWITTERBAR.prefs.getBoolPref("showTrends")) {
  261.                 try {
  262.                     if (!page.location.host.match(/^twitter\.com$/)) {
  263.                         return;
  264.                     }
  265.                 } catch (e) {
  266.                     return;
  267.                 }
  268.                 
  269.                 TWITTERBAR_COMMON.addTrends(page);
  270.             }
  271.         }
  272.     },
  273.     
  274.     buttonCheck : function () {
  275.         try {
  276.             var mode = TWITTERBAR.prefs.getBoolPref("button");
  277.             var button = document.getElementById("twitterBox");
  278.             
  279.             button.setAttribute("hidden", mode.toString());
  280.         } catch (e) { }
  281.         
  282.         try {
  283.             var mode = TWITTERBAR.prefs.getBoolPref("oneriotButton");
  284.             var button = document.getElementById("twitter-oneriot-box");
  285.             
  286.             button.setAttribute("hidden", mode.toString());
  287.         } catch (e) { }
  288.     },
  289.     
  290.     focus : function () {
  291.         var status = document.getElementById("urlbar").value;
  292.         
  293.         if (status.match(/^https?:\/\//i)) {
  294.             this.lastUrl = status;
  295.         }
  296.         
  297.         this.toolbarCount();
  298.     },
  299.     
  300.     reAuthorize : function () {
  301.         TWITTERBAR.prefs.setCharPref("oauth_username", "");
  302.         TWITTERBAR.prefs.setCharPref("access_token.oauth_token", "");
  303.         TWITTERBAR.prefs.setCharPref("access_token.oauth_token_secret", "");
  304.         TWITTERBAR.prefs.setCharPref("oauth_timestamp", "");
  305.         
  306.         this.oAuthorize();
  307.     },
  308.     
  309.     search : function (event, source) {
  310.         var status = document.getElementById("urlbar").value;
  311.         
  312.         if (status.match(/^(https?:\/\/[^\s]+)$/ig)) {
  313.             var search_terms = status;
  314.         }
  315.         else {
  316.             var search_terms = status.replace(/https?:\/\/[^\s]+/ig, "");
  317.         }
  318.  
  319.         search_terms = search_terms.replace(" --search", "");
  320.         
  321.         openUILink(TWITTERBAR_COMMON.getSearchURL(search_terms, source), event, false, true);
  322.     },
  323.     
  324.     oAuthorize : function () {
  325.         var accessor = {
  326.             consumerSecret : TWITTERBAR_COMMON.oauth.consumer_secret,
  327.             tokenSecret : ""
  328.         };
  329.         
  330.         var message = {
  331.             action : TWITTERBAR_COMMON.oauth.serviceProvider.requestTokenURL,
  332.             method : "GET",
  333.             parameters : [
  334.                 ["oauth_consumer_key",TWITTERBAR_COMMON.oauth.consumer_key],
  335.                 ["oauth_signature_method",TWITTERBAR_COMMON.oauth.serviceProvider.signatureMethod],
  336.                 ["oauth_version","1.0"]
  337.             ]
  338.         };
  339.         
  340.         var OAuth = TWITTERBAR_OAUTH();
  341.         
  342.         OAuth.setTimestampAndNonce(message);
  343.         OAuth.SignatureMethod.sign(message, accessor);
  344.         
  345.         var oAuthArgs = OAuth.getParameterMap(message.parameters);
  346.         var authHeader = OAuth.getAuthorizationHeader("http://twitter.com/", oAuthArgs);
  347.         
  348.         var req = new XMLHttpRequest();
  349.         req.mozBackgroundRequest = true;
  350.         req.open(message.method, message.action, true);
  351.         req.setRequestHeader("Authorization", authHeader);
  352.         
  353.         req.onreadystatechange = function () {
  354.             if (req.readyState == 4) {
  355.                 if (TWITTERBAR.debug) {
  356.                     TWITTERBAR.log("Auth ("+req.status+"): " + req.responseText);
  357.                 }
  358.                 
  359.                 if (req.status == 200) {
  360.                     var parts = req.responseText.split("&");
  361.                 
  362.                     try {
  363.                         TWITTERBAR_COMMON.oauth.request_token.oauth_token = parts[0].split("=")[1];
  364.                         TWITTERBAR_COMMON.oauth.request_token.oauth_token_secret = parts[1].split("=")[1];
  365.                         
  366.                         gBrowser.selectedBrowser.userTypedValue = TWITTERBAR.lastUrl;
  367.                         
  368.                         if (TWITTERBAR_COMMON.confirm(TWITTERBAR_COMMON.strings.getString("twitterbar.oauthRequest1") + "\n\n" + TWITTERBAR_COMMON.strings.getString("twitterbar.oauthRequest2"))) {
  369.                             getBrowser().selectedTab = getBrowser().addTab("http://twitter.com/oauth/authorize?oauth_token="+TWITTERBAR_COMMON.oauth.request_token.oauth_token);
  370.                         }
  371.                     } catch (e) {
  372.                         TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getString("twitterbar.oauthError1") + "\n\n" + e + "\n\n" + req.responseText);
  373.                     }
  374.                 }
  375.                 else if (req.status >= 500) {
  376.                     TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getString("twitterbar.failWhale"));
  377.                 }
  378.                 else {
  379.                     TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getFormattedString("twitterbar.otherError", [ req.status, req.responseText ]));
  380.                 }
  381.             }
  382.         };
  383.         
  384.         req.send(null);
  385.     },
  386.     
  387.     post : function (clickedOnButton) {
  388.         if (clickedOnButton && TWITTERBAR.prefs.getBoolPref("confirm")) {
  389.             if (!TWITTERBAR_COMMON.confirmPost()) {
  390.                 return;
  391.             }
  392.         }
  393.         
  394.         var image = document.getElementById("twitter-statusbarbutton");
  395.         image.src =  "chrome://twitterbar/skin/Throbber-small.gif";
  396.         
  397.         var urlbar = document.getElementById("urlbar");
  398.         var status = urlbar.value.replace("$$", content.document.title);
  399.         
  400.         if (status.match(/^https?:\/\/[^\s]+$/i)) {
  401.             this.lastUrl = status;
  402.             
  403.             var prefix = (TWITTERBAR.prefs.getCharPref("web").replace("$$", content.document.title).replace(/^\s+|\s+$/, "") + " ");
  404.             status = prefix + status;
  405.         }
  406.         
  407.         urlbar.value = TWITTERBAR_COMMON.strings.getString("twitterbar.posting");
  408.         
  409.         TWITTERBAR_SHORTENERS.shortenUrls(status, function (status) { TWITTERBAR.postRequest(status); });
  410.     },
  411.     
  412.     postRequest : function (status) {
  413.         if (TWITTERBAR.debug) {
  414.             TWITTERBAR.log("postRequest: " + status);
  415.         }
  416.         
  417.         TWITTERBAR.lastTweet = status;
  418.         
  419.         var accessor = {
  420.             consumerSecret : TWITTERBAR_COMMON.oauth.consumer_secret,
  421.             tokenSecret : TWITTERBAR.prefs.getCharPref("access_token.oauth_token_secret")
  422.         };
  423.  
  424.         var message = {
  425.             action : "http://twitter.com/statuses/update.xml",
  426.             method : "POST",
  427.             parameters : [
  428.                 ["oauth_consumer_key",TWITTERBAR_COMMON.oauth.consumer_key],
  429.                 ["oauth_token", TWITTERBAR.prefs.getCharPref("access_token.oauth_token")],
  430.                 ["oauth_signature_method",TWITTERBAR_COMMON.oauth.serviceProvider.signatureMethod],
  431.                 ["oauth_version","1.0"],
  432.                 ["source","twitterbar"],
  433.                 ["status", status]
  434.             ]
  435.         };
  436.         
  437.         if (status.length > 140) {
  438.             if (!TWITTERBAR_COMMON.confirm(
  439.                 TWITTERBAR_COMMON.strings.getFormattedString("twitterbar.tooLong", [status.length]))) {
  440.                 TWITTERBAR.afterPost(true);
  441.                 return;
  442.             }
  443.         }
  444.         
  445.         var OAuth = TWITTERBAR_OAUTH();
  446.         
  447.         OAuth.setTimestampAndNonce(message);
  448.         OAuth.SignatureMethod.sign(message, accessor);
  449.         
  450.         var argstring = "source=twitterbar&status=" + encodeURIComponent(status);
  451.  
  452.         var oAuthArgs = OAuth.getParameterMap(message.parameters);
  453.         var authHeader = OAuth.getAuthorizationHeader("http://twitter.com/", oAuthArgs);
  454.         
  455.         var req = new XMLHttpRequest();
  456.         req.mozBackgroundRequest = true;
  457.         req.open(message.method, message.action, true);
  458.         req.setRequestHeader("Authorization", authHeader);
  459.         req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  460.         req.setRequestHeader("Content-Length", argstring.length);
  461.         
  462.         req.onreadystatechange = function () {
  463.             if (req.readyState == 4) {
  464.                 if (TWITTERBAR.debug) {
  465.                     TWITTERBAR.log("Post ("+req.status+"): " + req.responseText);
  466.                 }
  467.                 
  468.                 if (req.status != 200 || TWITTERBAR.covertMode) {
  469.                     var image = document.getElementById('twitter-statusbarbutton');
  470.                     image.src =  "chrome://twitterbar/skin/bird-16-full.png";
  471.                 }
  472.                 
  473.                 if (req.status == 401) {
  474.                     if (req.responseText.indexOf("expired") != -1) {
  475.                         TWITTERBAR.reAuthorize();
  476.                     }
  477.                     else {
  478.                         TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getFormattedString("twitterbar.twitterError", [ req.status, req.responseText ]));
  479.                     }
  480.                     
  481.                     // I think TwitterBar sends a 401 when you've hit your rate limit.
  482.                     // This is the reason so many people complained about being asked to reauthorize.
  483.                 }
  484.                 else if (req.status >= 500) {
  485.                     TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getString("twitterbar.failWhale"));
  486.                 }
  487.                 else if (req.status == 200) {
  488.                     TWITTERBAR.lastTweet = null;
  489.                     
  490.                     if (!TWITTERBAR.covertMode) {
  491.                         document.getElementById("urlbar").value = TWITTERBAR_COMMON.strings.getString("twitterbar.success");
  492.                         
  493.                         var image = document.getElementById("twitter-statusbarbutton");
  494.                         image.src =  "chrome://twitterbar/skin/accept.png";
  495.                         
  496.                         setTimeout(function () { TWITTERBAR.afterPost(); }, 1000);
  497.                     }
  498.                 }
  499.                 else {
  500.                     TWITTERBAR_COMMON.alert(TWITTERBAR_COMMON.strings.getFormattedString("twitterbar.otherError", [ req.status, req.responseText ]));
  501.                 }
  502.                 
  503.                 TWITTERBAR.covertMode = false;
  504.             }
  505.         };
  506.         
  507.         req.send(argstring);
  508.     },
  509.     
  510.     afterPost : function (noSuccess) {
  511.         var urlbar = document.getElementById("urlbar");
  512.         
  513.         urlbar.value = this.lastUrl;
  514.         
  515.         gBrowser.selectedBrowser.userTypedValue = this.lastUrl;
  516.         
  517.         if (!noSuccess && TWITTERBAR.prefs.getBoolPref("tab")){
  518.             getBrowser().selectedTab = getBrowser().addTab("http://twitter.com/" + TWITTERBAR.prefs.getCharPref("oauth_username"));
  519.         }
  520.         
  521.         var image = document.getElementById('twitter-statusbarbutton');
  522.         image.src = "chrome://twitterbar/skin/bird-16-full.png";
  523.     },
  524.     
  525.     count : function () {
  526.         document.getElementById("twitter-searchbutton").hidden = false;
  527.         
  528.         var imagest = document.getElementById('twitter-statusbarbutton');
  529.         imagest.src = "chrome://twitterbar/skin/add.png"
  530.         
  531.         var count = document.getElementById('twitter-count');
  532.         count.hidden = false;
  533.         
  534.         var length = this.getCharCount();
  535.         count.value = (140 - length) + " Left";
  536.         
  537.         if (length > 140) {
  538.             count.style.color = "red";
  539.         }
  540.         else {
  541.             count.style.color = "green";
  542.         }
  543.     },
  544.     
  545.     countClear : function () {
  546.         var image = document.getElementById('twitter-statusbarbutton');
  547.         
  548.         if (image.src.match(/add\.png/)) {
  549.             image.src =  "chrome://twitterbar/skin/bird-16-full.png"
  550.         }
  551.         
  552.         var count = document.getElementById('twitter-count');
  553.         count.hidden = true;
  554.         document.getElementById("twitter-searchbutton").hidden = true;
  555.     },
  556.     
  557.     getCharCount : function () {
  558.         var status = document.getElementById("urlbar").value;
  559.         status = status.replace("$$", content.document.title);
  560.         
  561.         var length = status.length;
  562.         
  563.         var offset = 0;
  564.         
  565.         var urls = status.match(/(https?:\/\/[^\s]+)/ig);
  566.         
  567.         if (urls) {
  568.             for (var i = 0; i < urls.length; i++) {
  569.                 var urlLength = TWITTERBAR_SHORTENERS.getUrlLength(urls[i]);
  570.                 
  571.                 if (urls[i].length > urlLength) {
  572.                     offset += (urls[i].length - urlLength);
  573.                 }
  574.             }
  575.         }
  576.         
  577.         length -= offset;
  578.         
  579.         if (status.match(/^https?:\/\//i)) {
  580.             var prefix = (TWITTERBAR.prefs.getCharPref("web").replace("$$", content.document.title).replace(/^\s+|\s+$/, "") + " ");
  581.             length += prefix.length;
  582.         }
  583.         
  584.         return length;
  585.     },
  586.     
  587.     toolbarCount : function () {
  588.         var button = document.getElementById('twitter-toolbar-count');
  589.         
  590.         if (button) {
  591.             button.setAttribute('value', 140 - this.getCharCount());
  592.         }
  593.     },
  594.     
  595.     postKey : function (e) {
  596.         if (!e || (e.keyCode != e.DOM_VK_RETURN && e.keyCode != 117 && e.keyCode != 76 && e.keyCode != 68 && e.keyCode != 17 && e.keyCode != 18)){
  597.             var urlbar = document.getElementById("urlbar");
  598.             var status = urlbar.value;
  599.  
  600.             if (status.indexOf(" --post") != -1){
  601.                 var status = status.split(" --post")[0].replace("$$", content.document.title);
  602.                 
  603.                 if (status.match(/^https?:\/\//i)) {
  604.                     var webtext = (TWITTERBAR.prefs.getCharPref("web").replace("$$", content.document.title).replace(/^\s+|\s+$/, "") + " ");
  605.                     status = webtext + status;
  606.                 }
  607.                 
  608.                 status = status.replace("$$", content.document.title)
  609.                 
  610.                 urlbar.value = TWITTERBAR_COMMON.strings.getString("twitterbar.posting");
  611.                 
  612.                 TWITTERBAR_SHORTENERS.shortenUrls(status, function (status) { TWITTERBAR.postRequest(status); });
  613.                 
  614.                 var imagest = document.getElementById('twitter-statusbarbutton');
  615.                 imagest.src = "chrome://twitterbar/skin/Throbber-small.gif";
  616.             }
  617.             else if (status.indexOf("--options") != -1){
  618.                 urlbar.value = this.lastUrl;
  619.                 
  620.                 gBrowser.selectedBrowser.userTypedValue = this.lastUrl;
  621.                 
  622.                 this.openOptions();
  623.             }
  624.             else if (status.indexOf(" --search") != -1) {
  625.                 this.search(null, "addressBarText");
  626.             }
  627.         }
  628.         
  629.         this.toolbarCount();
  630.     },
  631.     
  632.     onButtonClick : function (e) {
  633.         if (e.button != 2){ 
  634.             TWITTERBAR.post(true);
  635.         }
  636.     },
  637.     
  638.     openOptions : function () {
  639.         openDialog('chrome://twitterbar/content/optionsDialog.xul', 'options', 'modal,centerscreen');
  640.     },
  641.  
  642.     log : function (message) {
  643.         var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  644.         consoleService.logStringMessage("TWITTERBAR: " + message);
  645.     }
  646. };